provider "alicloud" {
  region = "cn-beijing"
  # You'll need to configure access keys using environment variables or provider config
}

# 1. Create Kubernetes cluster for our applications
resource "alicloud_cs_managed_kubernetes" "fungo_cluster" {
  name                  = "fungo-cluster"
  version               = "1.20.11-aliyun.1"
  worker_vswitch_ids    = [alicloud_vswitch.vswitch.id]
  new_nat_gateway       = true
  worker_instance_types = ["ecs.g6.large"]
  worker_number         = 3
  
  # Enable auto scaling
  cluster_spec          = "ack.pro"
  
  # Enable logging
  addons {
    name = "flannel"
  }
  
  addons {
    name = "logtail-ds"
  }
}

# 2. Create MySQL RDS instance for order-service
resource "alicloud_db_instance" "order_mysql" {
  engine           = "MySQL"
  engine_version   = "8.0"
  instance_type    = "rds.mysql.s1.small"
  instance_storage = 20
  vswitch_id       = alicloud_vswitch.vswitch.id
  instance_name    = "fungo-order-db"
  
  # Enable backup
  backup_period = ["Monday", "Wednesday", "Friday"]
  backup_time   = "02:00Z-03:00Z"
}

# 3. Create Redis instance for user-service
resource "alicloud_kvstore_instance" "user_redis" {
  instance_name  = "fungo-user-redis"
  vswitch_id     = alicloud_vswitch.vswitch.id
  instance_class = "redis.master.small.default"
  engine_version = "5.0"
  instance_type  = "Redis"
  
  # Set password via variables
  password       = var.redis_password
}

# 4. Create ACR (Alibaba Cloud Container Registry) for our Docker images
resource "alicloud_cr_namespace" "fungo_namespace" {
  name               = "fungo"
  auto_create        = true
  default_visibility = "PUBLIC"
}

resource "alicloud_cr_repo" "user_service_repo" {
  namespace = alicloud_cr_namespace.fungo_namespace.name
  name      = "user-service"
  summary   = "User Service Repository"
  repo_type = "PUBLIC"
}

resource "alicloud_cr_repo" "order_service_repo" {
  namespace = alicloud_cr_namespace.fungo_namespace.name
  name      = "order-service"
  summary   = "Order Service Repository"
  repo_type = "PUBLIC"
} 